package com.graduation.mathematics.ui.activity;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.graduation.mathematics.R;
import com.graduation.mathematics.adapter.RankingAdapter;
import com.graduation.mathematics.base.BaseActivity;
import com.graduation.mathematics.bean.Ranking;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.XPopupImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import butterknife.BindView;
import butterknife.OnClick;

public class RankingActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_back)
    ImageView imgBack;

    @BindView(R.id.recycler_ranking)
    RecyclerView recycler_ranking;

    private RankingAdapter myAdapter;
    private List<Ranking> datas = new ArrayList<>();



    @Override
    protected int getLayout() {
        return R.layout.activity_statistics_ranking;
    }

    @Override
    protected void initDataAndEvent() {

        tvTitle.setText("成绩排名");

        recycler_ranking.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        myAdapter = new RankingAdapter(datas,this );
        recycler_ranking.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener((imageView, imageUrl) -> {
            new XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asImageViewer(imageView, imageUrl, new ImageLoader()).show();
        });
    }

    public static class ImageLoader implements XPopupImageLoader {

        @Override
        public void loadImage(int position, @NonNull Object uri, @NonNull ImageView imageView) {
            Glide.with(imageView).load(uri).apply(new RequestOptions().override(Target.SIZE_ORIGINAL)).into(imageView);
        }

        @Override
        public File getImageFile(@NonNull Context context, @NonNull Object uri) {
            try {
                return Glide.with(context).downloadOnly().load(uri).submit().get();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }


    @OnClick({R.id.img_back})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.img_back) {
            finish();
        }
    }
}
